package com.luguangtao.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luguangtao.config.Md5Util;
import com.luguangtao.entity.*;
import com.luguangtao.mapper.ClazzMapper;
import com.luguangtao.mapper.StudentMapper;
import com.luguangtao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @类名: com.luguangtao.controller
 * @作者: lgt
 * @创建时间: 2019-12-07 09:01
 * @描述:
 **/
@RestController
public class ClientController1 {

    @Autowired
    UserMapper userMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    ClazzMapper clazzMapper;


    //登录
    @RequestMapping("login1")
    public String login1(@RequestParam String username, @RequestParam String password){
        System.out.println(username+"============="+password);
        //解析密码加密
        //password = Md5Util.md5Encoding(password);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        List<User> users = userMapper.selectByExample(userExample);
        if(users!=null && users.size()>0){
            return "success";
        }
        return "error";
    }

    //注册
    @RequestMapping("regist1")
    public String regist1(@RequestBody User user){
        //注册密码加密
       // user.setPassword(Md5Util.md5Encoding(user.getPassword()));
        user.setType(0);
        userMapper.insert(user);
        return "success";
    }


    //列表
    @RequestMapping("lists1")
    public PageInfo<Student> lists1(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize){
        PageHelper.startPage(pageNum,3);
        StudentExample studentExample = new StudentExample();
        List<Student> students = studentMapper.selectByExample(studentExample);
        for (int i = 0; i <students.size() ; i++) {
            Student student = students.get(i);
            //获取班级
            Clazz clazz = clazzMapper.selectByPrimaryKey(student.getClazzid());
            student.setClazzname(clazz.getClazzname());
        }
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        return studentPageInfo;
    }

    //获取班级
    @RequestMapping("getClazz1")
    public List<Clazz> getClazz1(){
        List<Clazz> clazzes = clazzMapper.selectByExample(new ClazzExample());
        return clazzes;
    }

    //添加
    @RequestMapping("addStu1")
    public int addStu1(@RequestBody Student student){
        int aa = studentMapper.insert(student);
        if(aa!=0){
            return 1;
        }else{
            return 0;
        }
    }

    //删除
    @RequestMapping("del1")
    public int del1(@RequestParam Integer stuid){
        int aa = studentMapper.deleteByPrimaryKey(stuid);
        if(aa!=0){
            return 1;
        }else{
            return 0;
        }
    }

    //回显
    @RequestMapping("getStudent1")
    public Student getStudent1(@RequestParam Integer stuid){
        Student student = studentMapper.selectByPrimaryKey(stuid);
        return student;
    }

    //修改
    @RequestMapping("updateStu1")
    public int updateStu1(@RequestBody Student student){
        int aa = studentMapper.updateByPrimaryKey(student);
        if(aa!=0){
            return 1;
        }else{
            return 0;
        }
    }
}
